package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * @description:添加到购物车
     * @author: yanhongwei
     * @date: 2023/5/30 9:43
     * @param: [shoppingCart]
     * @return: com.itheima.reggie.common.R<com.itheima.reggie.entity.ShoppingCart>
     **/
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("shoppingCart:{}", shoppingCart);


        //设置用户id,指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        //判断是套餐还是菜品
        if (dishId != null) {
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);

        } else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        //判断是否查询到
        //如果没有查询到则进行保存，查询到则数量加1
        if (null != one) {
            Integer number = one.getNumber();
            one.setNumber(number + 1);

            shoppingCartService.updateById(one);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartService.save(shoppingCart);

            one = shoppingCart;
        }

        return R.success(one);
    }


    /**
     * @description:查询购物车数据
     * @author: yanhongwei
     * @date: 2023/5/31 9:52
     * @param: []
     * @return: com.itheima.reggie.common.R<java.util.List < com.itheima.reggie.entity.ShoppingCart>>
     **/
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);


        return R.success(list);
    }


    /**
     * @description:清空购物车
     * @author: yanhongwei
     * @date: 2023/5/31 9:52
     * @param: []
     * @return: com.itheima.reggie.common.R<java.util.List < com.itheima.reggie.entity.ShoppingCart>>
     **/
    @DeleteMapping("/clean")
    public R<String> clean() {
        log.info("清空购物车");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);


        return R.success("清空购物车成功");
    }

    /**
     * @description:减少购物车中的数量
     * @author: yanhongwei
     * @date: 2023/5/31 11:04
     * @param: [shoppingCart]
     * @return: com.itheima.reggie.common.R<com.itheima.reggie.entity.ShoppingCart>
     **/
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();

        //根据线程id查询用户id
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        //判断是否是菜品或者套餐
        if(null!=shoppingCart.getDishId()){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        //判断数量是否为最低值
        if (one.getNumber() <= 1) {
            throw new CustomException("已经到达最低数量!");
        }

        //进行数量的逻辑更新
        one.setNumber(one.getNumber() - 1);
        shoppingCartService.updateById(one);

        return R.success(one);

    }
}
